#!/usr/bin/env python
# coding: utf-8

# # 1 百度AI平台- OCR（光学字符识别）
# # 2 实践周
# 
# ## 通用文字识别
# * 接口描述
# > 在通用文字识别的基础上，提供更高精度的识别服务，支持更多语种识别（丹麦语、荷兰语、马来语、瑞典语、印尼语、波兰语、罗马尼亚语、土耳其语、希腊语、匈牙利语、泰语、越语、阿拉伯语、印地语及部分中国少数民族语言），并将字库从1w+扩展到2w+，能识别所有常用字和大部分生僻字。
# 
# * 请求示例
# > 1. HTTP 方法：POST 
# > 2. 请求URL： https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic
# 
# * 请求参数
# > 1. url参数-token
# > 2. 图片参数-图片文件
# 

# ### 创建Access_token

# In[4]:


import requests 

# client_id 为官网获取的AK， client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?'
# 准备url参数（酬载）
payload = {
    'grant_type':'client_credentials',
    'client_id':'6v0wAzsByuCEoB8MXujNG4RK',
    'client_secret':'ay8wpGky2GD7ciZrhgKBU0AWVqlnn94u'
    
    
}
response = requests.get(host, params=payload )
if response:
    print(response.json())
    access_token = response.json()['access_token']


# In[5]:


access_token


# ## 通用文字识别

# In[6]:


import requests
import base64

'''
通用物体和场景识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate"
# 二进制方式打开图片文件
# 1.图片文件准备
f = open('shu.jpg', 'rb')
img = base64.b64encode(f.read())

# 2. 酬载准备
payload={
    'access_token':access_token,
    'image':img
}

headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# ## 网络图片艺术字识别

# In[7]:


# encoding:utf-8
import requests
import base64

'''
网络图片文字识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage"
# 二进制方式打开图片文件
f = open('zi.jpg', 'rb')
img = base64.b64encode(f.read())

payload={
    'access_token':access_token,
    'image':img
}


headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# ## 手写字体识别

# In[8]:


# encoding:utf-8

import requests
import base64

'''
手写文字识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"
# 二进制方式打开图片文件
f = open('shouxiezi.jpg', 'rb')
img = base64.b64encode(f.read())

payload={
    'access_token':access_token,
    'image':img
}

headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# ## 卡片类文字识别身份证-银行卡

# In[9]:


# encoding:utf-8

import requests
import base64

'''
银行卡识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard"
# 二进制方式打开图片文件
f = open('yinhangka.jpg', 'rb')
img = base64.b64encode(f.read())

payload={
    'access_token':access_token,
    'image':img,
    'detect_direction':'true'
}

headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# ## 身份证正反识别

# In[10]:


import requests
import base64

'''
身份证识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
# 二进制方式打开图片文件
# 1.图片文件准备
f = open('shenfenzheng.jpg', 'rb')
img = base64.b64encode(f.read())

# 2. 酬载准备
payload={
    'access_token':access_token,
    'image':img,
    'id_card_side':'back'
}

headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# ## 二维码识别

# In[11]:


# encoding:utf-8

import requests
import base64

'''
二维码识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode"
# 二进制方式打开图片文件
f = open('erweima.png', 'rb')
img = base64.b64encode(f.read())

payload={
    'access_token':access_token,
    'image':img
}

headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# ## 财务类文字识别-增值税发票识别

# In[12]:


# encoding:utf-8

import requests
import base64

'''
增值税发票识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice"
# 二进制方式打开图片文件
f = open('fapiao.jpg', 'rb')
img = base64.b64encode(f.read())

payload={
    'access_token':access_token,
    'image':img
}


headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=payload, headers=headers)
if response:
    print (response.json())


# In[ ]:




